import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_business_core/business_customer/less_components/business_customer_details_wx_icon.dart';
import 'package:x3_business_core/business_customer/less_components/business_customer_item_avatar.dart';

class BusinessCustomerItem extends StatelessWidget {
  BusinessCustomerItem({Key key, @required this.item, this.isShowSubInfo = true}) : super(key: key);

  final dynamic item;
  final bool isShowSubInfo;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 4),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          BusinessCustomerItemAvatar(data: item),
          SizedBox(width: 12.0),
          Expanded(child: _buildVipItemComponentContent(context)),
        ],
      ),
    );
  }

  ///类型识别 - 当服务返回来的是0，会识别为0.0,但是实际是double
  static double intToDouble(dynamic data) {
    if (data == null) return 0.0;
    if (data.runtimeType == int) {
      return (data as int).toDouble();
    } else {
      if (data.runtimeType == String) return double.parse(data);
      return data;
    }
  }

  Widget _buildVipItemComponentContent(BuildContext context) {
    String vipName = BaseUtils.getKeyData(this.item, 'vipName').toString();
    String gradeName = BaseUtils.getKeyData(this.item, 'gradeName').toString();
    String vipPurse = BaseUtils.getKeyData(this.item, 'vipPurse').toString();
    String buyPurse = this.item['buyPurse'] == null ? '' : BaseDefaultsValue.doubleDefaults(this.item['buyPurse'], toStringAsFixedFractionDigits: 2).toString();
    String totalConsumeSsums = this.item['totalConsumeSsums'] == null ? '' : BaseDefaultsValue.doubleDefaults(this.item['totalConsumeSsums'], toStringAsFixedFractionDigits: 2).toString();
    String vipScoreInt = (intToDouble(this.item['vipScore'])).toInt().toString();
    String vipCouponNum = BaseUtils.getKeyData(this.item, 'vipCouponNum').toString();

    String notInteractiveDays = BaseUtils.getKeyData(this.item, 'notInteractiveDays', defaultValue: '-').toString();
    String notServiceDays = BaseUtils.getKeyData(this.item, 'notServiceDays', defaultValue: '-').toString();
    String notConsumedDays = BaseUtils.getKeyData(this.item, 'notConsumedDays', defaultValue: '-').toString();
    String notBuyDays = BaseUtils.getKeyData(this.item, 'notBuyDays', defaultValue: '-').toString();
    String notToDepotDays = BaseUtils.getKeyData(this.item, 'notToDepotDays', defaultValue: '-').toString();
    String notComeDays = BaseUtils.getKeyData(this.item, 'notComeDays', defaultValue: '-').toString();

    String initNotInteractiveDays = notInteractiveDays + '天';
    String initNotServiceDays = notServiceDays + '天';
    String initNotConsumedDays = (notConsumedDays == '-' ? notBuyDays : notConsumedDays) + '天';
    String initNotToDepotDays = (notToDepotDays == '-' ? notComeDays : notToDepotDays) + '天';
    double h = 4;
    return DefaultTextStyle(
      style: Theme.of(context).textTheme.subtitle2,
      child: Column(
        children: [
          Row(
            children: [
              BaseComponentUtils.buildTextSpanWidget([vipName, gradeName], styles: {0: Theme.of(context).textTheme.bodyText2.copyWith(fontSize: 15), 1: Theme.of(context).textTheme.subtitle1.copyWith(color: Color(0xFFBF9C4F))}),
              BusinessCustomerDetailsWxIcon(item: item),
            ],
          ),
          SizedBox(height: 4.0),
          Row(
            children: [
              Expanded(
                flex: isShowSubInfo ? 3 : 1,
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    BaseComponentUtils.buildTextSpanWidget(['储值金额:', vipPurse]),
                    SizedBox(height: h),
                    BaseComponentUtils.buildTextSpanWidget(['消费金额:', buyPurse.isNotEmpty ? buyPurse : totalConsumeSsums]),
                    SizedBox(height: h),
                    BaseComponentUtils.buildTextSpanWidget(['积分:', vipScoreInt]),
                    SizedBox(height: h),
                    BaseComponentUtils.buildTextSpanWidget(['优惠券:', vipCouponNum]),
                  ],
                ),
              ),
              if (isShowSubInfo)
                Expanded(
                  flex: 2,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      BaseComponentUtils.buildTextSpanWidget(['未互动:', '$initNotInteractiveDays']),
                      SizedBox(height: h),
                      BaseComponentUtils.buildTextSpanWidget(['未服务:', '$initNotServiceDays']),
                      SizedBox(height: h),
                      BaseComponentUtils.buildTextSpanWidget(['未消费:', '$initNotConsumedDays']),
                      SizedBox(height: h),
                      BaseComponentUtils.buildTextSpanWidget(['未到店:', '$initNotToDepotDays']),
                    ],
                  ),
                ),
            ],
          )
        ],
      ),
    );
  }
}
